//
//  QRCodeViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "QRCodeViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface QRCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property ( strong , nonatomic ) AVCaptureDevice * device;

@property ( strong , nonatomic ) AVCaptureDeviceInput * input;

@property ( strong , nonatomic ) AVCaptureMetadataOutput * output;

@property ( strong , nonatomic ) AVCaptureSession * session;

@property ( strong , nonatomic ) AVCaptureVideoPreviewLayer * preview;

@property ( nonatomic ) CGRect rectOfInterest;



@end

@implementation QRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Device
    
    _device = [ AVCaptureDevice defaultDeviceWithMediaType : AVMediaTypeVideo ];
    
    // Input
    
    _input = [ AVCaptureDeviceInput deviceInputWithDevice : self . device error : nil ];
    
    // Output
    
    _output = [[ AVCaptureMetadataOutput alloc ] init ];
    
    [ _output setMetadataObjectsDelegate : self queue : dispatch_get_main_queue ()];
    
    _output.rectOfInterest=CGRectMake(0.5,0,0.5, 1);
    
    // Session
    
    _session = [[ AVCaptureSession alloc ] init ];
    
    [ _session setSessionPreset : AVCaptureSessionPresetHigh ];
    
    if ([ _session canAddInput : self . input ])
        
    {
        
        [ _session addInput : self . input ];
        
    }
   
        
        [ _session addOutput : self . output ];
    
    
    // 条码类型 AVMetadataObjectTypeQRCode
    
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode] ;
    
    // Preview
    
    _preview =[ AVCaptureVideoPreviewLayer layerWithSession : _session ];
    
    _preview . videoGravity = AVLayerVideoGravityResizeAspectFill ;
    
    _preview . frame = self . view . layer . bounds ;
    
    [ self . view . layer insertSublayer : _preview atIndex : 0 ];
    
    // Start
    
    [ _session startRunning ];
}
//    然后实现 AVCaptureMetadataOutputObjectsDelegate
    
#pragma mark AVCaptureMetadataOutputObjectsDelegate

    
    - ( void )captureOutput:( AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:( AVCaptureConnection *)connection
    
    {
        
        NSString *stringValue;
        
        if ([metadataObjects count ] > 0 )
            
        {
            
            // 停止扫描
            
            [ _session stopRunning ];        
            
            AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
            
            stringValue = metadataObject. stringValue ;
            NSLog(@"%@",stringValue);
            
        }
        
    }
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
